package cn.tradewin.pmp.pages.test;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.TextField;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;

public class MixedValidation {
	
	 @Property
	    @Persist(PersistenceConstants.FLASH)
	    private String firstName;

	    @Property
	    @Persist(PersistenceConstants.FLASH)
	    private String lastName;

	    // Generally useful bits and pieces

	    @Component(id = "inputs")
	    private Form form;

	    @Component(id = "firstName")
	    private TextField firstNameField;

	    @Component(id = "lastName")
	    private TextField lastNameField;
	    
	    @Inject
	    Logger log;

	    // The code

	    @Log
	    void onValidateFromInputs() {

	        // Error if the names don't contain letters only

	        if (firstName != null) {
	            if (!firstName.matches("[A-Za-z]+")) {
	                form.recordError(firstNameField, "First Name must contain letters only");
	            }
	        }

	        if (lastName != null) {
	            if (!lastName.matches("[A-Za-z]+")) {
	                form.recordError(lastNameField, "Last Name must contain letters only");
	            }
	        }
	        log.debug("------------------" + firstName);
	        
	    }
	    
	    @Log
	    @OnEvent(value=EventConstants.PREPARE_FOR_SUBMIT)
	    void onPreparForSubmit() {
	    	firstName = "zhouyc123";
	    }
	    
	    @Log
	    void onPrepare() {
	    	
	    }
	    
	    @Log
	    @OnEvent(value=EventConstants.SUBMIT)
	    void onSubmit() {
	    	
	    }
	    
	    @Log
	    void onSuccess() {
	    	
	    }
	    
	    @Log
	    void onFailure() {
	    	
	    }
}
